package com.example.chengyu.testmvvm.recyclerTest;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.chengyu.testmvvm.R;
import com.example.chengyu.testmvvm.databinding.ActivitySecondBinding;

import java.util.ArrayList;
import java.util.List;

public class SecondActivity extends AppCompatActivity {

    private ActivitySecondBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_second);
        init();
    }

    private void init() {
        List<Bean> mList = getNewsList();
        initRecyclerView(mList);
    }

    private void initRecyclerView(final List<Bean> mList) {
        RecyclerView mRecyclerView = mBinding.recyclerNews;
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        MyAdapter adapter = new MyAdapter(mList);
        adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int id, Bean bean) {
                Toast.makeText(SecondActivity.this, "item"+id, Toast.LENGTH_SHORT).show();
                mList.get(id).setContent(mList.get(id).getContent()+"\nadd something");
            }
        });
        mRecyclerView.setAdapter(adapter);
    }

    private List<Bean> getNewsList() {
        List<Bean> list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Bean bean = new Bean("title" + i,"content" + i);
            list.add(bean);
        }
        return list;
    }
}
